package com.xidian.zhiancloud.service.accidentManage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.AccidentMessageMapper;
import com.xidian.zhiancloud.dao.AccidentReviewMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.AccidentMessage;
import com.xidian.zhiancloud.model.AccidentReview;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.accidentmessage.AccidentMessageQO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentMessageVO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AccidentReviewService extends ServiceImpl<AccidentReviewMapper,AccidentReview> implements IService<AccidentReview> {

    @Resource
    private AccidentReviewMapper accidentReviewMapper;

    @Resource
    private AccidentMessageMapper accidentMessageMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private OrganizationMapper organizationMapper;

    /**
     * 添加事故信息
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean addAccidentReviewMsg(AccidentReview accidentReview){

        accidentReviewMapper.insert(accidentReview);

        AccidentMessage message = accidentMessageMapper.selectById(accidentReview.getAccidentId());

        if (message == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }

        if(accidentReview.getResult() == 0){
            message.setReviewStatus(3);
        }
        if(accidentReview.getResult() == 1){
            message.setReviewStatus(2);
        }
        accidentMessageMapper.updateById(message);

        return true;
    }

    /**
     * 更新事故审核信息
     * @param accidentReviewVO
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean updateAccidentReviewMsg(AccidentReviewVO accidentReviewVO){
        AccidentReview accidentReview = new AccidentReview();
        BeanUtils.copyProperties(accidentReviewVO,accidentReview);
        accidentReviewMapper.updateById(accidentReview);
        return true;
    }

    /**
     * 根据id查询事故审核信息
     * @param id 事故id
     * @return 返回事故审核信息列表
     */
    public List<AccidentReviewVO> getAccidentReviewById(Long id){
        LambdaQueryWrapper<AccidentReview> query = Wrappers.lambdaQuery();
        query.eq(AccidentReview::getAccidentId,id);
        List<AccidentReview> list = accidentReviewMapper.selectList(query);

        List<AccidentReviewVO> new_list = new ArrayList<>();

        list.forEach(entry -> {
            AccidentReviewVO vo = new AccidentReviewVO();
            BeanUtils.copyProperties(entry,vo);

            SysUser reviewer = sysUserMapper.selectById(vo.getReviewerId());
            Organization organization = organizationMapper.selectById(vo.getDepartId());

            vo.setReviewName(reviewer == null ? "该用户不存在" : reviewer.getName());
            vo.setReviewDepartment(organization == null ? "该部门不存在" : organization.getName());
            new_list.add(vo);
        });


        return new_list;
    }

    /**
     * 列表查询
     * @param accidentMessageQO
     * @return
     */
    public IPage<AccidentMessageVO> getListByQO(AccidentMessageQO accidentMessageQO){

        Page<AccidentMessageVO> page = new Page<>(accidentMessageQO.getCurrent(),accidentMessageQO.getSize());

        //排序
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);

        IPage<AccidentMessageVO> list = accidentMessageMapper.getListByQO(page, accidentMessageQO);

        list.getRecords().forEach(
                vo -> {
                    vo.setDepartmentName(vo.getDepartmentName()==null ? "该部门不存在" : vo.getDepartmentName());
                    vo.setCreatorName(vo.getCreatorName()==null ? "该用户不存在" : vo.getCreatorName());

                    List<AccidentReviewVO> accidentReviewVOS = getAccidentReviewById(vo.getId());

                    vo.setAccidentReviewList(accidentReviewVOS);
                }
        );

        return list;
    }

}
